CLASS zcl_form_generate DEFINITION
  PUBLIC
  FINAL
  CREATE PUBLIC .

  PUBLIC SECTION.
    METHODS generate_form
      IMPORTING
        iv_form_name         TYPE string
        iv_node_key_fielname TYPE string
        iv_node_key_value    TYPE string
      EXPORTING
        ev_message           TYPE bapi_msg
        ev_type              TYPE bapi_mtype
        eo_pdf               TYPE ze_file_raw.
  PROTECTED SECTION.
  PRIVATE SECTION.

ENDCLASS.



CLASS ZCL_FORM_GENERATE IMPLEMENTATION.


  METHOD generate_form.

    SELECT SINGLE * FROM ztbc_form_temp WHERE formtemplate = @iv_form_name INTO @DATA(ls_ztbc_form_temp).

    IF sy-subrc <> 0.
      ev_type = 'E'.
      ev_message = '没有维护对应的表单模板，请检查'.
      RETURN.
    ENDIF.

    TRY.
        "#获取Service Definition数据服务类
        DATA(lo_fdp_util) = cl_fp_fdp_services=>get_instance( ls_ztbc_form_temp-servicedifination ).
        "#key
        DATA(lt_keys)     = lo_fdp_util->get_keys( ).
        "#KEY字段赋值
        lt_keys[ name = iv_node_key_fielname ]-value = iv_node_key_value.
        "#读取数据
        DATA(lv_xml) = lo_fdp_util->read_to_xml( lt_keys ).
      CATCH cx_fp_fdp_error INTO DATA(lv_error).
        "handle exception
    ENDTRY.

    TRY.
        cl_fp_ads_util=>render_pdf(
            EXPORTING
              iv_xml_data    = lv_xml
              iv_xdp_layout  = ls_ztbc_form_temp-xdp_attachment
              iv_locale      = 'zh_CN'
              is_options     = VALUE #( trace_level = 4 )"Use 0 in production environment
            IMPORTING
               ev_pdf          = DATA(lv_pdf)
               ev_trace_string = DATA(lv_trace)
               ev_pages        = DATA(lv_ppages)
            ).
        eo_pdf = lv_pdf.
        ev_type = 'S'.
      CATCH cx_fp_ads_util INTO DATA(lv_ads_error).
        "handle exception
    ENDTRY.


  ENDMETHOD.
ENDCLASS.
